package com.huluobo.a2203parkdemo.repair

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.text.TextUtils
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ToastUtils
import com.google.gson.Gson
import com.huluobo.a2203parkdemo.R
import com.huluobo.a2203parkdemo.adapter.RepairAdapter
import com.huluobo.a2203parkdemo.adapter.RepairUserAdapter
import com.huluobo.a2203parkdemo.databinding.ActivityRepairBinding
import com.huluobo.base.bean.RepairBean
import com.huluobo.base.bean.RepairBeanItem
import com.huluobo.base.mvvm.BaseActivity
import com.huluobo.base.net.Const
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
class RepairActivity : BaseActivity<ActivityRepairBinding, RepairViewModel>() {

    private lateinit var repairAdapter: RepairAdapter
    private var repairList = mutableListOf<RepairBeanItem>()
    private var isAllSelect: Boolean = false
    private val repairUserList = mutableListOf("张师傅", "李师傅", "刘师傅", "毛师傅")

    private lateinit var repairUserAdapter: RepairUserAdapter
    override fun initData() {
        viewModel.getRepairList()

        viewModel.getRepairListSuccess.observe(this) {
            val repairList = Gson().fromJson(it, RepairBean::class.java)
            this.repairList.addAll(repairList)
            repairAdapter.data.addAll(repairList)
            repairAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        binding.repairHeader.headerEditTv.visibility = View.VISIBLE
        binding.repairHeader.headerTitle.text = "维修列表"
        binding.repairHeader.headerBack.setOnClickListener { finish() }

        binding.repairRv.layoutManager = LinearLayoutManager(this)
        repairAdapter = RepairAdapter()
        binding.repairRv.adapter = repairAdapter

        repairAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, RepairDetailActivity::class.java)
            intent.putExtra(Const.PARAM_REPAIR_ID, repairList[position].repairId)
            intent.putExtra("time", repairList[position].repairTime)
            startActivity(intent)
        }

        binding.repairUserRv.layoutManager = LinearLayoutManager(this)
        repairUserAdapter = RepairUserAdapter()
        binding.repairUserRv.adapter = repairUserAdapter

        repairUserAdapter.setOnItemClickListener { adapter, view, position ->
            (adapter.getViewByPosition(
                position,
                R.id.item_repair_user_tv
            ) as TextView).setBackgroundResource(R.drawable.repair_user_shape)
        }

        binding.repairUserBtn.setOnClickListener {
            binding.repairBottomCl.visibility = View.GONE
            ToastUtils.showLong("已指定师傅维修")
        }

        binding.repairHeader.headerEditTv.setOnClickListener {
            if (TextUtils.equals(binding.repairHeader.headerEditTv.text, "编辑")) {
                binding.repairHeader.headerEditTv.text = "完成"
                showSelect(true)
                binding.repairBottomCl.visibility = View.VISIBLE
                repairUserAdapter.data.addAll(repairUserList)
                repairUserAdapter.notifyDataSetChanged()
            } else {
                binding.repairHeader.headerEditTv.text = "编辑"
                showSelect(false)
                binding.repairBottomCl.visibility = View.GONE
                repairUserAdapter.data.clear()
            }
        }


        binding.repairSelectAllImg.setOnClickListener {
            if (isAllSelect) {
                isAllSelect = false
                isSelect(isAllSelect)
                binding.repairSelectAllImg.setImageResource(R.drawable.gou_default)
            } else {
                isAllSelect = true
                isSelect(isAllSelect)
                binding.repairSelectAllImg.setImageResource(R.drawable.gou)
            }
        }

    }

    private fun showSelect(isShow: Boolean) {
        repairList.forEach {
            it.isShow = isShow
        }
        repairAdapter.data.addAll(repairList)
        repairAdapter.notifyDataSetChanged()
    }

    private fun isSelect(isSelected: Boolean) {
        repairList.forEach {
            it.isSelected = isSelected
        }
        repairAdapter.data.addAll(repairList)
        repairAdapter.notifyDataSetChanged()
    }
}